package com.jl.card.provider.jl2g;

import java.nio.charset.Charset;

class STKUtil {

	public static String parseItemText(byte[] value, int offset, int length) {
		switch (value[offset]) {
		case (byte) 0x80:
			return new String(value, offset + 1, length - 1,
					Charset.forName("UTF-16BE"));
		case (byte) 0x81:
		case (byte) 0x82:
		case (byte) 0x83:
		default:
			return new String(value, offset, length,
					Charset.forName("US-ASCII"));
		}
	}

	public static byte[] makeTextWithCodingByte(String data, byte coding) {
		byte[] text = null;
		if (coding == (byte) 0x80 || coding == (byte) 0x08) {
			text = data.getBytes(Charset.forName("UTF-16BE"));
		} else if (coding == (byte) 0x04) {
			text = data.getBytes(Charset.forName("US-ASCII"));
		} else {
			text = data.getBytes(Charset.forName("US-ASCII"));
		}
		byte[] res = new byte[text.length + 1];
		res[0] = coding;
		System.arraycopy(text, 0, res, 1, text.length);
		return res;
	}
}

